package com.fiume.consumer.web;

import com.fiume.consumer.client.UserClient;
import com.fiume.consumer.pojo.Order;
import com.fiume.consumer.pojo.Result;
import com.fiume.consumer.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * Description:
 * </p>
 *
 * @author fiume
 * @version v1.0.0
 * @create 2021-01-17 22:00
 * @see com.fiume.consumer.web
 */
@RestController
@RequestMapping("order")
public class ConsumerController {

    //---@Autowired
    //---private RestTemplate restTemplate;

    //--@Autowired
    //--private DiscoveryClient discoveryClient;

    @Resource
    private UserClient userClient;

    @GetMapping("/{id}")
    //----@HystrixCommand(fallbackMethod = "findOriderByIdfallback")
    public Result findOrderByUserId(@PathVariable("id") Long userId) {
        //1.准备用户信息
//        1.1 准备url地址
        //--List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
//        1.1.2 获取一个eureka客户机实例
        //--ServiceInstance instance = instances.get(0);
//        1.1.3 获得该实例的uri地址
        //--URI uri = instance.getUri();
        //-String url = "http://127.0.0.1:8081/user/" + userId;
//        1.1.4 拼接字符串
        //--String url = String.format("%s/user/%s", uri, userId);
        //---String url = String.format("http://user-service/user/%s", userId);

//        1.2 通过http请求从服务端拿到id对应的user实例
        //---User user = restTemplate.getForObject(url, User.class);

        //1.2-update 通过feign完成本地伪装调用服务端拿到id对应的user实例
        User user = userClient.findUserByid(userId);
        //2.通过用户id查询到订单信息并返回
        Order order = new Order(23L, user.getId(), user.getName(), 123456L);

        return Result.ok(order);
    }

    //3.当服务出错时返回的参数
    /*----public Result findOriderByIdfallback(Long id) {
               return Result.error("查询用户[" + id + "]的订单失败");
    }*/

}
